import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:vap_plugin/vap_plugin_view.dart';
import 'package:get/get.dart';
import 'package:flutter/services.dart' show rootBundle;

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  GlobalKey<VapPluginViewState> playerKey = GlobalKey<VapPluginViewState>();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('vap_plugin example'),
        ),
        body: SafeArea(
          child: Container(
            color: Colors.blue,
            child: Stack(
              children: [
                Positioned(
                    width: 375,
                    height: 413,
                    child: Container(
                      color: Colors.black38,
                      child: IgnorePointer(
                        // VapView可以通过外层包Container(),设置宽高来限制弹出视频的宽高
                        // VapView can set the width and height through the outer package Container() to limit the width and height of the pop-up video
                        child:
                            // FittedBox(
                            //   fit: BoxFit.cover,
                            //   child:
                            SizedBox(
                          width: 375,
                          height: 413,
                          child: VapPluginView(
                            key: playerKey,
                            playEndCallback: () {},
                          ),
                        ),

                        // ),
                      ),
                    )),
                Positioned(
                  left: 0,
                  bottom: 0,
                  width: 100,
                  child: TextButton(
                      onPressed: () async {
                        // https://static.cdyiqingljicp.cn/upload/head/1628223400.80423222852856-6EAD-445D-BC06-2B581531C328.jpg
                        // https://static.cdyiqingljicp.cn/upload/video/bd7626b7-e01d-466d-90fd-e2f6b4691a11.mp4
                        Uint8List bytes = await imageAssetToUint8List('assets/qq.png');
                        var loopCount = 2;
                        if (Platform.isIOS && loopCount > 0) {
                          loopCount -= 1;
                        }
                        playerKey.currentState?.playByAsset({
                          'path': "assets/video3.mp4",
                          'loopCount': loopCount,
                          'autoDestroy': true,
                          'width': 375,
                          'height': 413,
                          '[imgUser]': bytes,
                          '[imgAnchor]': bytes,
                          '[textAnchor]': '主播名字测试',
                          '[textUser]': '用户名字测试',
                        });
                      },
                      child: const Text("点击 播放")),
                ),
                Positioned(
                  left: 110,
                  bottom: 0,
                  width: 100,
                  child: TextButton(
                      onPressed: () async {
                        playerKey.currentState?.stopPlay();
                      },
                      child: const Text("点击 停止")),
                ),
                Positioned(
                  left: 220,
                  bottom: 0,
                  width: 100,
                  child: TextButton(
                      onPressed: () async {
                        playerKey.currentState?.manualDestroy();
                      },
                      child: const Text("点击 销毁")),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Future<Uint8List> imageAssetToUint8List(String assetPath) async {
    ByteData byteData = await rootBundle.load(assetPath);
    Uint8List uint8List = byteData.buffer.asUint8List();
    return uint8List;
  }
}
